package com.atguigu.flink.chapter11;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Json;
import org.apache.flink.table.descriptors.Kafka;
import org.apache.flink.table.descriptors.Schema;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/1/22 10:35
 */
public class Flink04_Table_Connector_kafka {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        Schema schema = new Schema()
            .field("id", DataTypes.STRING())
            .field("ts", DataTypes.BIGINT())
            .field("vc", DataTypes.INT());
        
        tEnv
            .connect(
                new Kafka()
                    .version("universal")
                    .property("bootstrap.servers", "hadoop162:9092")
                    .property("group.id", "Flink04_Table_Connector_kafka")
                    .startFromLatest()
                    .topic("s1")
            )
            //            .withFormat(new Csv())
            .withFormat(new Json())
            .withSchema(schema)
            .createTemporaryTable("sensor");
        
        Table result = tEnv.from("sensor")
            .where($("id").isEqual("sensor_1"))
            .select($("id"), $("vc"));
        
        // 建立一张表与kafka的topic进行关联
        tEnv
            .connect(
                new Kafka()
                    .version("universal")
                    .property("bootstrap.servers", "hadoop162:9092")
                    .topic("s2")
                    .sinkPartitionerRoundRobin()
            )
            //            .withFormat(new Csv())
//            .withFormat(new Csv().lineDelimiter(""))
            .withFormat(new Json())
            .withSchema(new Schema()
                            .field("id", DataTypes.STRING())
                            .field("vc", DataTypes.INT()))
            .createTemporaryTable("result_sensor");
    
        result.executeInsert("result_sensor");
    }
}
